<?php

/**
 * @version     1.5
 * @package	CSME ~ RSS Feed 2.0
 * @copyright	Copyright (C) 2011 LBN Studio. All rights reserved.
 * @author      Jose Luis Quintana S.
 * @link        http://www.lbn.pe/
 */
include 'dbconfig.php';
include 'administration/Libraries/Tools/FeedWriter.php';

defined('JL') or die('Access denied!');

$title_actualites = 'Club Sportif Multisections d\'Epinay-sur-Seine ~ Actualités';
$title = 'Club Sportif Multisections d\'Epinay-sur-Seine';
$description = 'L\'info du CSME en temps réel.';
$image = JLPATH_BASE . '/images/id_csme.png';
$author = 'CSME :: Club Sportif Multisections d\'Epinay-sur-Seine)';
$language = 'fr-fr';
$time = date(DATE_RSS, time());

$feed = new FeedWriter(RSS2);
$feed->setTitle($title_actualites);
$feed->setLink(JLPATH_BASE);
$feed->setDescription($description);
/* $feed->setImage($title, JLPATH_BASE,
  $image, 166, 50); */
$feed->setChannelElement('language', $language);
$feed->setChannelElement('lastBuildDate', date(DATE_RSS, time()));
$feed->setChannelElement('generator', 'LBN Studio');
$feed->setChannelElement('ttl', 5);


$sql = "SELECT
  tbl_actualites.int_actualite,
  tbl_actualites.var_ac_title,
  tbl_actualites.txt_content_actualite,
  tbl_actualites.dat_creation_act,
  tbl_sections.int_id_section,
  tbl_sections.var_desc_section
FROM
  tbl_sections
  INNER JOIN tbl_actualites ON (tbl_sections.int_id_section = tbl_actualites.int_id_section)
WHERE
  tbl_actualites.int_id_state <> 3
ORDER BY
  tbl_actualites.int_actualite DESC
LIMIT 400";

$res = mysql_query($sql, $cnn);

$sql = 'SELECT var_monthyear FROM
    tbl_actualites
WHERE int_id_section < 3 AND int_id_section <> 0 AND int_id_state <> 3
GROUP BY var_monthyear
ORDER BY int_actualite DESC
LIMIT 200';

$res_g = mysql_query($sql, $cnn);
$ord = 1;
while ($fila = mysql_fetch_assoc($res_g)) {
    $groupmonth[$fila['var_monthyear']] = $ord;
    $ord++;
}

while ($row = mysql_fetch_assoc($res)) {
    $title = utf8_decode($row['var_desc_section'] . ' ~ ' . $row['var_ac_title']);
    $title = html_entity_decode($title);

    $description = utf8_decode($row['txt_content_actualite']);

    $description = str_replace('&nbsp;', " ", $description);
    $description = str_replace('\'', "'", $description);
    $description = str_replace('’', "'", $description);
    $description = str_replace('/?', "?", $description);

    $description = str_replace('&gt;', ">", $description);
    $description = str_replace('&lt;', "<", $description);

    $time = rss_date_fr(utf8_decode($row['dat_creation_act']));

    $newItem = $feed->createNewItem();
    $newItem->setTitle($title);
    $newItem->setDescription($description);

    $ide = intval(trim($row['int_id_section'])) - 2;
    $idact = $row['int_actualite'];

    $SQL = "SELECT COUNT(`int_id_gallery`) AS 'num_gall' FROM `tbl_gallery`
        WHERE int_id_state <> 3 AND int_num_photos <> 0
        AND int_id_sections_view = " . $ide;
    $resk = mysql_query($SQL, $cnn);
    $rowk = mysql_fetch_assoc($resk);

    if (intval($rowk['num_gall']) > 0)
        $_t = intval($rowk['num_gall']) + 5;
    else
        $_t = 5;

    if ($ide <= 0) {
        $link = JLPATH_BASE . '/actualites.html?act=' . $idact . '#' . $groupmonth[$row['var_monthyear']];
    } else {
        $link = JLPATH_BASE . '/section.html?id=' . $ide . "&act=" . $idact . "#" . $_t;
    }

    $newItem->setLink($link);

    $newItem->setDate($time);
    $newItem->addElement('author', $author);
    $newItem->addElement('dc:creator', $author);
    $feed->addItem($newItem);
}

$feed->genarateFeed();

mysql_free_result($res);
mysql_close($cnn);

function rss_date_fr($string_date) {
    $months = array("Janvier", "Février", "Mars", "Avril", "Mai", "Juin", "Juillet",
        "Août", "Septembre", "Octobre", "Novembre", "Décembre");
    $months_replace = array("Janvier" => 01, "Février" => 02, "Mars" => 03, "Avril" => 04, "Mai" => 05,
        "Juin" => 06, "Juillet" => 07, "Août" => 08, "Septembre" => 09, "Octobre" => 10,
        "Novembre" => 11, "Décembre" => 12);
    $days = array("Dimanche", "Lundi", "Mardi", "Mercredi", "Jeudi", "Vendredi", "Samedi");

    $date = str_replace($months, $months_replace, $string_date);
    $date = trim(str_replace($days, '', $date));

    if (preg_match('/([0-9]{1,2}) ([0-9]{1,2}) ([0-9]{2,4})/', $date)) {
        $date = explode(' ', $date);
        $time = mktime(10, 15, 35, $date[1], $date[0], $date[2], 1);
        $date = date(DATE_RSS, $time);
    } else {
        $date = date(DATE_RSS, time());
    }
    return $date;
}